package com.esun.service.mq.rabbitmq.listener;

import com.alibaba.fastjson.JSONObject;
import com.esun.dao.das.DatabaseLogDas;
import com.esun.dao.eo.DatabaseLogEo;
import com.esun.dao.eo.OperationLogEo;
import com.esun.service.component.DatabaseLogComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author John.xiao
 * @date 2021/10/12 17:29
 */
@Component
@RabbitListener(queues = "databaseLog")
@Slf4j
public class DatabaseLogListener {
	@Resource
	DatabaseLogDas databaseLogDas;

	@Resource
	DatabaseLogComponent databaseLogComponent;

	@RabbitHandler
	public void process(String message){
		log.info("接收到数据日志:{}",JSONObject.parseObject(message, DatabaseLogEo.class));
		try {
			DatabaseLogEo databaseLog  = JSONObject.parseObject(message,DatabaseLogEo.class);
			databaseLogDas.insert(databaseLog);
		}
		catch (Exception e){
			e.printStackTrace();
			Date now = new Date();
			Long time = now.getTime();
			databaseLogComponent.setErrorLogs(time,message);
		}
	}



}
